package com.renren.rrwx;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;

import com.renren.mobile.rmsdk.core.RMConnectCenter;
import com.renren.rrwx.model.HeroManager;
import com.renren.rrwx.view.HeroListView;
import com.renren.rrwx.view.HeroPropView;

/*
 * 游戏的主界面
 * 
 */
public class GameActivity extends Activity {

	public static String TAG = "Wuxia";

	/**
	 * 游戏的几种状态
	 * 
	 */
	public final static int STATE_PLAYER = 1;
	public final static int STATE_HERO_LIST = 2;
	private int state = STATE_PLAYER;

	// 一些UI控件
	FrameLayout gameFrame;
	// 两个界面
	HeroPropView myPropView;
	HeroListView heroListView;
	// 两个按钮
	Button mpBtn, herosBtn;
	// 人人连接
	RMConnectCenter center;
	private boolean hasLogin = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game);
		// 检查当前有没有登录？？
		center = RMConnectCenter.getInstance(this);
		hasLogin = center.hasLogin();
		if (!hasLogin) {
			// 跳转初始界面
			Intent i = new Intent(this, RenrenWuXiaActivity.class);
			this.startActivity(i);
			finish();
		}
		state = STATE_PLAYER;
		// 初始化UI控件
		initGameUI(savedInstanceState);
		//
		initGameData();

	}

	private void initGameData() {
		//
		this.myPropView.setHero(HeroManager.getInstance(this).getPlayer());

	}

	public final static int UI_MY_PROP = 0;
	public final static int UI_HERO_LIST = 1;
	private int state_ui = -1;

	public void setUIState(int s) {
		if (state_ui != s) {
			state_ui = s;
			myPropView.setVisibility(state_ui == UI_MY_PROP ? View.VISIBLE
					: View.INVISIBLE);
			heroListView.setVisibility(state_ui == UI_HERO_LIST ? View.VISIBLE
					: View.INVISIBLE);
		}
	}

	private void initGameUI(Bundle savedInstanceState) {
		// 游戏节目的布局
		gameFrame = (FrameLayout) findViewById(R.id.game_frame);
		// 我的属性界面
		myPropView = new HeroPropView(this);
		gameFrame.addView(myPropView);
		heroListView = new HeroListView(this);
		gameFrame.addView(heroListView);
		//
		mpBtn = (Button) findViewById(R.id.game_btn_myprop);
		mpBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setUIState(UI_MY_PROP);
			}

		});
		herosBtn = (Button) findViewById(R.id.game_btn_heros);
		herosBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setUIState(UI_HERO_LIST);
			}

		});
		setUIState(UI_MY_PROP);
	}

	public static void LOGD(String msg) {
		Log.d(TAG, msg);
	}
}
